package com.anycc.cloud.core.entity;

import com.anycc.cloud.common.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 机构实体类
 */
@Entity
@Table(
        name = "sys_org",
        indexes = {@Index(
                columnList = "name",
                name = "i_sys_org_name"
        ), @Index(
                columnList = "code",
                name = "i_sys_org_code"
        )}
)
public class Org extends BaseEntity {
        private static final long serialVersionUID = 6222872162585322722L;
        public static final String FIELD_NAME = "name";
        public static final String FIELD_TYPE = "type";
        public static final String FIELD_PARENT = "parent";
        public static final String FIELD_CHILDREN = "children";
        public static final String FIELD_ROLES = "roles";
        public static final String FIELD_AREA = "area";
        public static final String FIELD_CODE = "code";
        public static final String FIELD_LON = "lon";
        public static final String FIELD_LAT = "lat";
        private String name;
        private OrgType type;
        private Org parent;
        private List<Org> children = new ArrayList();
        private List<Role> roles = new ArrayList();
        private Area area;
        private String code;
        private Double lon;
        private Double lat;

        public Org() {
        }

        @Column(
                name = "name",
                length = 64,
                nullable = false
        )
        public String getName() {
                return this.name;
        }

        public void setName(String name) {
                this.name = name;
        }

        @ManyToOne
        @JoinColumn(
                name = "type"
        )
        @JsonIgnoreProperties({"updateTime", "createTime"})
        public OrgType getType() {
                return this.type;
        }

        public void setType(OrgType type) {
                this.type = type;
        }

        @ManyToOne
        @JoinColumn(
                name = "parent_id"
        )
        @JsonIgnoreProperties({"children", "parent", "updateTime", "createTime", "roles"})
        public Org getParent() {
                return this.parent;
        }

        public void setParent(Org parent) {
                this.parent = parent;
        }

        @OneToMany(
                mappedBy = "parent"
        )
        @OrderBy("name")
        @JsonIgnore
        public List<Org> getChildren() {
                return this.children;
        }

        public void setChildren(List<Org> children) {
                this.children = children;
        }

        @ManyToMany
        @JoinTable(
                name = "sys_org_role",
                joinColumns = {@JoinColumn(
                        name = "org_id",
                        referencedColumnName = "id"
                )},
                inverseJoinColumns = {@JoinColumn(
                        name = "role_id",
                        referencedColumnName = "id"
                )}
        )
        @JsonIgnoreProperties({"permissions", "description", "updateTime", "createTime"})
        public List<Role> getRoles() {
                return this.roles;
        }

        public void setRoles(List<Role> roles) {
                this.roles = roles;
        }

        @ManyToOne
        @JoinColumn(
                name = "area_id"
        )
        @JsonIgnoreProperties({"parent", "children", "updateTime", "createTime"})
        public Area getArea() {
                return this.area;
        }

        public void setArea(Area area) {
                this.area = area;
        }

        @Column(
                name = "code",
                length = 36,
                unique = true
        )
        public String getCode() {
                return this.code;
        }

        public void setCode(String code) {
                this.code = code;
        }

        @Column(
                name = "lon"
        )
        public Double getLon() {
                return this.lon;
        }

        public void setLon(Double lon) {
                this.lon = lon;
        }

        @Column(
                name = "lat"
        )
        public Double getLat() {
                return this.lat;
        }

        public void setLat(Double lat) {
                this.lat = lat;
        }
}